package com.tjmc.company.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjmc.company.api.entity.AutoRefreshLogEntity;
import com.tjmc.company.service.AutoRefreshLogService;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;

import com.tjmc.recruit.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.tjmc.recruit.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 职位自动刷新日志
 *
 * @author recruit
 * @date 2024-12-06 15:16:28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/autoRefreshLog")
@Tag(description = "autoRefreshLog", name = "职位自动刷新日志管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AutoRefreshLogController {

    private final AutoRefreshLogService autoRefreshLogService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param autoRefreshLog 职位自动刷新日志
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R getAutoRefreshLogPage(@ParameterObject Page page, @ParameterObject AutoRefreshLogEntity autoRefreshLog) {
        LambdaQueryWrapper<AutoRefreshLogEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(autoRefreshLogService.page(page, wrapper));
    }


    /**
     * 通过条件查询职位自动刷新日志
     *
     * @param autoRefreshLog 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询", description = "通过条件查询对象")
    @GetMapping("/details")
    public R getDetails(@ParameterObject AutoRefreshLogEntity autoRefreshLog) {
        return R.ok(autoRefreshLogService.list(Wrappers.query(autoRefreshLog)));
    }

    /**
     * 新增职位自动刷新日志
     *
     * @param autoRefreshLog 职位自动刷新日志
     * @return R
     */
    @Operation(summary = "新增职位自动刷新日志", description = "新增职位自动刷新日志")
    @SysLog("新增职位自动刷新日志")
    @PostMapping
    @Inner
    public R save(@RequestBody AutoRefreshLogEntity autoRefreshLog) {
        return R.ok(autoRefreshLogService.save(autoRefreshLog));
    }

    @Operation(summary = "查询当日刷新次数", description = "查询当日刷新次数")
    @SysLog("查询当日刷新次数")
    @GetMapping("/getDayCount")
    @Inner
    public R getDayCount(@RequestBody AutoRefreshLogEntity autoRefreshLog) {
        long count = autoRefreshLogService.count(new LambdaQueryWrapper<AutoRefreshLogEntity>()
                .eq(ObjectUtil.isNotNull(autoRefreshLog.getCompanyId()), AutoRefreshLogEntity::getCompanyId, autoRefreshLog.getCompanyId())
                .eq(ObjectUtil.isNotNull(autoRefreshLog.getPersonalUserId()), AutoRefreshLogEntity::getPersonalUserId, autoRefreshLog.getPersonalUserId())
                .ge(AutoRefreshLogEntity::getCreatedAt, DateUtil.beginOfDay(new Date())));
        return R.ok(count);
    }

}
